package com.joinlabs.action;


import java.io.File;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.OrderSystemer;
import com.joinlabs.entity.PageBean;
import com.joinlabs.entity.User;
import com.joinlabs.entity.Wares;
import com.joinlabs.entity.conditionBean;
import com.joinlabs.service.IOrderService;
import com.joinlabs.service.IProductSystemer;
import com.joinlabs.service.IUserSystemer;

@Controller
@RequestMapping("/systemer")
@SessionAttributes(names="pages")
public class SystemerAction {
	
	
	@Resource
	private IProductSystemer WaresGroService;
	@Resource 
	private IUserSystemer UserSystemerService;
	@Resource
	private IOrderService OrderService;
//商品上架界面显示
	@RequestMapping("/gljmspsj")
	public ModelAndView gljmspsj(conditionBean condition,ModelMap map) {
		int a = condition.getCurPage();
		if(a <= 0) {
			a = 1;
		}
		//System.out.println("方法前：");
		int waresGro = 0;
		List<Wares> Waress =  WaresGroService.findInfobyGrounding(waresGro);
		//map.addAttribute("pros",Waress);
				PageBean pageBean = new PageBean();
				//System.out.println(condition.getCurPage());
				pageBean.setCurPage(a);
				//System.out.println(pageBean.getCurPage());
				//一页显示的数量
				pageBean.setPageCount(8);
				//开始到结束的下标
				pageBean.calcIndex();
				//System.out.println(pageBean.getEndIndex());
				//总共有多少页
				pageBean.calcPages(Waress.size());
				
				map.addAttribute("Waress", Waress);
				//System.out.println(Waress.size());
				map.addAttribute("pages", pageBean);
		//System.out.println("方法后：");
		return new ModelAndView ("/systemer/gljm_spsj");
	}
	
//商品批量上架
	@RequestMapping("/gljmspsjsj")
	public ModelAndView gljmspsjsj(String[] id,ModelMap map) {
		int a = 0;
		WaresGroService.updateProsbyPids(id , a);
		
		//System.out.println(id);
		
		//System.out.println("方法后：");
		
		
		return new ModelAndView ( "redirect:/systemer/gljmspsj");
	}
	
//spsj搜索商品
	@RequestMapping("/gljmspsjss")
	public ModelAndView gljmspsjss(String waresName,ModelMap map) {
		if(waresName != "") {
			List<Wares> Waress = WaresGroService.selectInfobywaresName(waresName);
			map.addAttribute("Waress",Waress);
			//System.out.println("方法后：");
			return new ModelAndView ("/systemer/gljm_spsj");
			
		}
		return new ModelAndView ( "redirect:/systemer/gljmspsj");
		
	}
	
//商品下架界面显示
	@RequestMapping("/gljmspxj")
	public ModelAndView gljmspxj(conditionBean condition,ModelMap map) {
		int a = condition.getCurPage();
		if(a <= 0) {
			a = 1;
		}
		//System.out.println("方法前：");
		int waresGro = 1;
		List<Wares> Waress =  WaresGroService.findInfobyGrounding(waresGro);
		//map.addAttribute("pros",Waress);
		PageBean pageBean = new PageBean();
		//System.out.println(condition.getCurPage());
		pageBean.setCurPage(a);
		//System.out.println(pageBean.getCurPage());
		//一页显示的数量
		pageBean.setPageCount(8);
		//开始到结束的下标
		pageBean.calcIndex();
		//System.out.println(pageBean.getEndIndex());
		//总共有多少页
		pageBean.calcPages(Waress.size());
		
		map.addAttribute("Waress", Waress);
		//System.out.println(Waress.size());
		map.addAttribute("pages", pageBean);
		//System.out.println("方法后：");
		return new ModelAndView ("/systemer/gljm_spxj");
	}
//商品批量下架
	@RequestMapping("/gljmspxjxj")
	public ModelAndView gljmspxjxj(String[] id,ModelMap map) {
		int a = 1;
		WaresGroService.updateProsbyPids(id , a);
		
		System.out.println(id);
		
		//System.out.println("方法后：");
		
		
		return new ModelAndView ( "redirect:/systemer/gljmspxj");
	}
	
//spxj搜索商品
	@RequestMapping("/gljmspxjss")
	public ModelAndView gljmspxjss(String waresName,ModelMap map) {
		if(waresName != "") {
			List<Wares> Waress = WaresGroService.selectInfobywaresName(waresName);
			map.addAttribute("Waress",Waress);
			//System.out.println("方法后：");
			return new ModelAndView ("/systemer/gljm_spxj");
			
		}
		return new ModelAndView ( "redirect:/systemer/gljmspxj");
	}	

//商品删除界面显示
		@RequestMapping("/gljmscsp")
		public ModelAndView gljmscsp(conditionBean condition,ModelMap map) {
			int a = condition.getCurPage();
			if(a <= 0) {
				a = 1;
			}
			List<Wares> Waress =  WaresGroService.findInfoAll();
			System.out.println("方法前：");
			//查询分页实体
					PageBean pageBean = new PageBean();
					//System.out.println(condition.getCurPage());
					pageBean.setCurPage(a);
					//System.out.println(pageBean.getCurPage());
					//一页显示的数量
					pageBean.setPageCount(8);
					//开始到结束的下标
					pageBean.calcIndex();
					System.out.println(pageBean.getEndIndex());
					//总共有多少页
					pageBean.calcPages(Waress.size());
					
					map.addAttribute("Waress", Waress);
					System.out.println(Waress.size());
					map.addAttribute("pages", pageBean);
					
					System.out.println(pageBean);
			//System.out.println(Waress.size());
			System.out.println("方法后：");
			return new ModelAndView ("/systemer/gljm_scsp");
		}
		
//商品批量删除
	@RequestMapping("/gljmscscsp")
	public ModelAndView gljmscscsp(String[] id,ModelMap map) {
		
		WaresGroService.delProsbyPids(id);
		
		//System.out.println(id);
		
		//System.out.println("方法后：");
		
		
		return new ModelAndView ( "redirect:/systemer/gljmscsp");
	}
//scsp搜索商品
	@RequestMapping("/gljmscspss")
	public ModelAndView gljmscspss(String waresName,ModelMap map) {
		if(waresName != "") {
		List<Wares> Waress = WaresGroService.selectInfobywaresName(waresName);
		map.addAttribute("Waress",Waress);
		//System.out.println("方法后：");
		return new ModelAndView ("/systemer/gljm_scsp");
		}
		return new ModelAndView ( "redirect:/systemer/gljmscsp");
	}


//添加商品	界面显示	
	@RequestMapping("/gljmtjsp")
	public ModelAndView gljmtjsp(Wares wares, ModelMap map) {
		//System.out.println(wares.toString());
		
		
		return new ModelAndView ("/systemer/gljm_tjsp");
	}
//添加商品
	@RequestMapping("/gljmtjspsp")
	public ModelAndView gljmtjspsp(Wares wares, ModelMap map) {
		//System.out.println(wares.toString());
		//遍历多个文件
		
		System.out.println(wares.getUploadFile().getSize());
		MultipartFile multipartFile = wares.getUploadFile();
				
						//处理你的文件
						
						//获取文件的基本信息(名字,类型,大小)
						String fileName = multipartFile.getOriginalFilename();
						System.out.println(fileName);
						//String a = "wares/"+fileName;
						wares.setImgA(fileName);
						try {
							
							//文件名
							//uploadFile.getName() 获取 input的 name属性的
							
							//文件上传名
							String contentType = multipartFile.getContentType();
							long size = multipartFile.getSize();
							System.out.println(fileName+"-"+contentType+"-"+size);
							
							//判断文件类型(只能上传图片类型)
							if(!contentType.contains("image")){
								map.put("typemsg", "只能上传图片类型");
								throw new RuntimeException("非图片类型");
							}
							
							
							//文件上传 到 指定路径
							//IO工具包
							
							/*
							FileUtils.copyInputStreamToFile(
									uploadFile.getInputStream(), 
									new File("d://server/"+fileName));
							 */

							
							multipartFile.transferTo(new File("D:/Imgproject/Joinlabs/wares/"+fileName));
							
							map.put("uploadmsg", "文件上传成功");
							
						} catch (Exception e) {
							map.put("uploadmsg", "文件上传失败");
							e.printStackTrace();
						}
						
										
						
					
		  WaresGroService.insertProbyInfo(wares);

		return new ModelAndView ("redirect:/systemer/gljmscsp");
	}
//添加库存界面显示	
	@RequestMapping("/gljmtjkc")
	public ModelAndView gljmtjkc(conditionBean condition,ModelMap map) {
		//System.out.println("方法前：");
//	List<Wares> Waress =  WaresGroService.findInfoAll();
//	map.addAttribute("Waress", Waress);	
		int a = condition.getCurPage();
		if(a <= 0) {
			a = 1;
		}
		List<Wares> Waress =  WaresGroService.findInfobywaresUser();
		System.out.println("方法前：");
		//查询分页实体
				PageBean pageBean = new PageBean();
				//System.out.println(condition.getCurPage());
				pageBean.setCurPage(a);
				//System.out.println(pageBean.getCurPage());
				//一页显示的数量
				pageBean.setPageCount(8);
				//开始到结束的下标
				pageBean.calcIndex();
				System.out.println(pageBean.getEndIndex());
				//总共有多少页
				pageBean.calcPages(Waress.size());
				
				map.addAttribute("Waress", Waress);
				System.out.println(Waress.size());
				map.addAttribute("pages", pageBean);
				
				System.out.println(pageBean);
		//System.out.println(Waress.size());
		//System.out.println("方法后：");
		return new ModelAndView ("/systemer/gljm_tjkc");
	}
	
//添加库存	
		@RequestMapping("/gljmtjkckc")
		public ModelAndView gljmtjkckc(BigDecimal pid, int number,ModelMap map) {
			//System.out.println("方法前：");
			System.out.println(pid);
			System.out.println(number);
			Wares wares =WaresGroService.findInfobyPid(pid);
			//System.out.println(wares.toString());
			long a = wares.getWaresCount() + number;
			wares.setWaresCount(a);
			WaresGroService.updateProCountbyPid(wares);
			//System.out.println(a);
			
//		List<Wares> Waress =  WaresGroService.findInfoAll();
//		map.addAttribute("Waress", Waress);	
			//System.out.println("方法后：");
			return new ModelAndView ("redirect:/systemer/gljmtjkc");
		}	
//tjkc搜索商品
		@RequestMapping("/gljmtjkcss")
		public ModelAndView gljmtjkcss(String waresName,ModelMap map) {
			if(waresName != "") {
				List<Wares> Waress = WaresGroService.selectInfobywaresName(waresName);
				map.addAttribute("Waress",Waress);
				//System.out.println("方法后：");
				return new ModelAndView ("/systemer/gljm_tjkc");
				
			}
			return new ModelAndView ( "redirect:/systemer/gljmtjkc");
		}
		
//商品发货界面显示		
		@RequestMapping("/gljmspfh")
		public ModelAndView gljmspfh(conditionBean condition,ModelMap map) {
			List<OrderSystemer> OrderSystemers= UserSystemerService.findOrderbyorderState();
			//System.out.println(OrderSystemers.size());
			int a = condition.getCurPage();
			if(a <= 0) {
				a = 1;
			}
			//List<Wares> Waress =  WaresGroService.findInfoAll();
			System.out.println("方法前：");
			//查询分页实体
					PageBean pageBean = new PageBean();
					//System.out.println(condition.getCurPage());
					pageBean.setCurPage(a);
					//System.out.println(pageBean.getCurPage());
					//一页显示的数量
					pageBean.setPageCount(8);
					//开始到结束的下标
					pageBean.calcIndex();
					System.out.println(pageBean.getEndIndex());
					//总共有多少页
					pageBean.calcPages(OrderSystemers.size());
					
					map.addAttribute("OrderSystemers", OrderSystemers);
					//System.out.println(Waress.size());
					map.addAttribute("pages", pageBean);
					
					System.out.println(pageBean);
			//System.out.println(Waress.size());
			//System.out.println("方法后：");
			//return new ModelAndView ("/systemer/gljm_tjkc");
			return new ModelAndView ("/systemer/gljm_spfh");
		}		
//商品发货	
		@RequestMapping("/gljmspfhfh")
		public ModelAndView gljmspfhfh(String[] id,ModelMap map) {
			//int a = 1;
			//System.out.println(id);
			UserSystemerService.updateOrderStatebyOrderId(id);
			//System.out.println("方法后：");
			
			
			return new ModelAndView ( "redirect:/systemer/gljmspfh");
		}
		
//spfh搜索商品
		@RequestMapping("/gljmspfhss")
		public ModelAndView gljmspfhss(String waresName,ModelMap map) {
			if(waresName != "") {
				List<Wares> Waress = WaresGroService.selectInfobywaresName(waresName);
				map.addAttribute("Waress",Waress);
				//System.out.println("方法后：");
				return new ModelAndView ("/systemer/gljm_spfh");
				
			}
			return new ModelAndView ( "redirect:/systemer/gljmspfh");
			
		}		
		
		
		
		
		
//删除用户界面显示		
	@RequestMapping("/gljmscyh")
	public ModelAndView gljmscyh(conditionBean condition,ModelMap map) {
	
		int a = condition.getCurPage();
		if(a <= 0) {
			a = 1;
		}
		//System.out.println("方法前：");
		List<User>Users = 	UserSystemerService.findUsersinfoAll(1);
		//map.addAttribute("pros",Waress);
				PageBean pageBean = new PageBean();
				//System.out.println(condition.getCurPage());
				pageBean.setCurPage(a);
				//System.out.println(pageBean.getCurPage());
				//一页显示的数量
				pageBean.setPageCount(8);
				//开始到结束的下标
				pageBean.calcIndex();
				//System.out.println(pageBean.getEndIndex());
				//总共有多少页
				pageBean.calcPages(Users.size());
				
				map.addAttribute("Users", Users);
				//System.out.println(Waress.size());
				map.addAttribute("pages", pageBean);
		//System.out.println("方法后：");
	//map.addAttribute("Users",Users);
	return new ModelAndView ("/systemer/gljm_scyh");
	}
	
//用户批量删除
@RequestMapping("/gljmscscyh")
public ModelAndView gljmscscyh(String[] id,ModelMap map) {
	System.out.println(id);
	//WaresGroService.delProsbyPids(id);
	UserSystemerService.delUsersbyUserid(id);
	
	
	//System.out.println("方法后：");
	
	
	return new ModelAndView ( "redirect:/systemer/gljmscyh");
}
//scyh搜索用户
@RequestMapping("/gljmscyhss")
public ModelAndView gljmscyhss(String userName,ModelMap map) {
	if(userName != "") {
	List<User> Users = UserSystemerService.selectInfobywaresName(userName);
	System.out.println(Users);
	map.addAttribute("Users",Users);
	//System.out.println("方法后：");
	return new ModelAndView ("/systemer/gljm_scyh");
	}
	return new ModelAndView ( "redirect:/systemer/gljmscyh");
}
	
//删除管理员界面显示		
	@RequestMapping("/gljmscgly")
	public ModelAndView gljmscgly(conditionBean condition,ModelMap map) {
	
		int a = condition.getCurPage();
		if(a <= 0) {
			a = 1;
		}
		//System.out.println("方法前：");
		List<User>Users = 	UserSystemerService.findUsersinfoAll(0);
		//map.addAttribute("pros",Waress);
				PageBean pageBean = new PageBean();
				//System.out.println(condition.getCurPage());
				pageBean.setCurPage(a);
				//System.out.println(pageBean.getCurPage());
				//一页显示的数量
				pageBean.setPageCount(8);
				//开始到结束的下标
				pageBean.calcIndex();
				//System.out.println(pageBean.getEndIndex());
				//总共有多少页
				pageBean.calcPages(Users.size());
				
				map.addAttribute("Users", Users);
				//System.out.println(Waress.size());
				map.addAttribute("pages", pageBean);
		//System.out.println("方法后：");
	//map.addAttribute("Users",Users);
	return new ModelAndView ("/systemer/gljm_scgly");
	}
	
//管理员批量删除
@RequestMapping("/gljmscscgly")
public ModelAndView gljmscscgly(String[] id,ModelMap map) {
	System.out.println(id);
	//WaresGroService.delProsbyPids(id);
	UserSystemerService.delUsersbyUserid(id);
	
	
	//System.out.println("方法后：");
	
	
	return new ModelAndView ( "redirect:/systemer/gljmscgly");
}
//scgly搜索用户
@RequestMapping("/gljmscglyss")
public ModelAndView gljmscglyss(String userName,ModelMap map) {
	if(userName != "") {
	List<User> Users = UserSystemerService.selectInfobywaresName(userName);
	System.out.println(Users);
	map.addAttribute("Users",Users);
	//System.out.println("方法后：");
	return new ModelAndView ("/systemer/gljm_scgly");
	}
	return new ModelAndView ( "redirect:/systemer/gljmscgly");
}	
//添加管理员界面显示		
@RequestMapping("/gljmtjgly")
public ModelAndView gljmtjgly(ModelMap map) {
	
	return new ModelAndView ("/systemer/gljm_tjgly");
}	
		
		
//添加管理员	
@RequestMapping("/gljmtjtjgly")
public ModelAndView gljmtjtjgly(User user ,ModelMap map) {
	//System.out.println(user.toString());
	UserSystemerService.insertInfobyUser(user);
	return new ModelAndView ("redirect:/systemer/gljmtjgly");
}	

//用户添加商品	界面显示	
	@RequestMapping("/usertjsp")
	public ModelAndView usertjsp(ModelMap map) {
		//System.out.println(wares.toString());
		
		
		return new ModelAndView ("/systemer/user_tjsp");
	}

//用户添加商品
	@RequestMapping("/usertjspsp")
	public ModelAndView usertjspsp(Wares wares, ModelMap map) {
		//System.out.println(wares.toString());
		//遍历多个文件
		
		System.out.println(wares.getUploadFile().getSize());
		MultipartFile multipartFile = wares.getUploadFile();
				
						//处理你的文件
						
						//获取文件的基本信息(名字,类型,大小)
						String fileName = multipartFile.getOriginalFilename();
						System.out.println(fileName);
						//String a = "wares/"+fileName;
						wares.setImgA(fileName);
						try {
							
							//文件名
							//uploadFile.getName() 获取 input的 name属性的
							
							//文件上传名
							String contentType = multipartFile.getContentType();
							long size = multipartFile.getSize();
							System.out.println(fileName+"-"+contentType+"-"+size);
							
							//判断文件类型(只能上传图片类型)
							if(!contentType.contains("image")){
								map.put("typemsg", "只能上传图片类型");
								throw new RuntimeException("非图片类型");
							}
							
							
							//文件上传 到 指定路径
							//IO工具包
							
							/*
							FileUtils.copyInputStreamToFile(
									uploadFile.getInputStream(), 
									new File("d://server/"+fileName));
							 */

							
							multipartFile.transferTo(new File("D:/Imgproject/Joinlabs/wares/"+fileName));
							
							map.put("uploadmsg", "文件上传成功");
							
						} catch (Exception e) {
							map.put("uploadmsg", "文件上传失败");
							e.printStackTrace();
						}
						
										
						
					
		  WaresGroService.insertuserProbyInfo(wares);

		return new ModelAndView ("redirect:/systemer/usertjsp");
	}
	
	
	@RequestMapping("/malldetail")
	public ModelAndView selectOrdercount(ModelMap map) {
		//System.out.println(wares.toString());
		
		int row = OrderService.selectOrdercount();
		
		int zmoney = UserSystemerService.selectOrderiteminfoAll();
		
		int zuser = UserSystemerService.findUsersinfoAlluser();

		map.addAttribute("row",row);
		map.addAttribute("zmoney",zmoney);
		map.addAttribute("zuser",zuser);
		return new ModelAndView ("/systemer/gljm_jezh");
	}
	
}
